package com.hui.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.entity.dao.BaseDomain;
import com.hui.entity.dao.StoreProductReply;
import com.hui.entity.enums.ShopCommonEnum;
import com.hui.entity.vo.ReplyCountVo;
import com.hui.entity.vo.StoreCartQueryVo;
import com.hui.entity.vo.StoreProductReplyQueryVo;
import com.hui.mapper.StoreProductReplyMapper;
import com.hui.service.StoreProductReplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论表(StoreProductReply)表服务实现类
 *
 * @author hui
 * @since 2022-07-17 15:47:56
 */
@Service("storeProductReplyService")
public class StoreProductReplyServiceImpl extends ServiceImpl<StoreProductReplyMapper, StoreProductReply> implements StoreProductReplyService {

    @Override
    public int productReplyCount(long productId) {
        LambdaQueryWrapper<StoreProductReply> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StoreProductReply::getProductId, productId);
        //Math toIntExact() 将作为int返回的参数
        return Math.toIntExact(count(wrapper));
    }

    @Override
    public StoreProductReplyQueryVo getReply(long productId) {
        StoreProductReplyQueryVo vo = ((StoreProductReplyMapper) this.baseMapper).getReply(productId);
        return ObjectUtil.isNotNull(vo) ? this.handleReply(((StoreProductReplyMapper) this.baseMapper).getReply(productId)) : null;
    }

    @Override
    public String replyPer(long productId) {
        LambdaQueryWrapper<StoreProductReply> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StoreProductReply::getProductId, productId).eq(BaseDomain::getIsDel, ShopCommonEnum.DELETE_0.getValue()).eq(StoreProductReply::getProductScore, 5);
        int productScoreCount = Math.toIntExact(((StoreProductReplyMapper) this.baseMapper).selectCount(wrapper));
        int count = this.productReplyCount(productId);
        return count > 0 ? "" + NumberUtil.round(NumberUtil.mul(NumberUtil.div((float) productScoreCount, (float) count), 100.0F), 2) : "0";
    }

    @Override
    public List<StoreProductReplyQueryVo> getReplyList(long productId, int type, int page, int limit) {
        List<StoreProductReplyQueryVo> newList = new ArrayList();
        Page<StoreProductReply> pageModel = new Page((long) page, (long) limit);
        List<StoreProductReplyQueryVo> list = ((StoreProductReplyMapper) this.baseMapper).selectReplyList(pageModel, productId, type);
        List<StoreProductReplyQueryVo> list1 = (List) list.stream().map((i) -> {
            StoreProductReplyQueryVo vo = new StoreProductReplyQueryVo();
            BeanUtils.copyProperties(i, vo);
            if (i.getPictures().contains(",")) {
                vo.setPics(i.getPictures().split(","));
            }

            return vo;
        }).collect(Collectors.toList());

        for (StoreProductReplyQueryVo queryVo : list1) {
            newList.add(this.handleReply(queryVo));
        }

        return newList;
    }

    @Override
    public ReplyCountVo getReplyCount(long productId) {
        int sumCount = this.productReplyCount(productId);
        if (sumCount == 0) {
            return new ReplyCountVo();
        } else {
            //TODO 计划优化
            int goodCount = Math.toIntExact(((StoreProductReplyMapper) this.baseMapper).selectCount(new LambdaQueryWrapper<StoreProductReply>().eq(StoreProductReply::getProductId, productId).eq(StoreProductReply::getProductScore, 5)));
            int inCount = Math.toIntExact(((StoreProductReplyMapper) this.baseMapper).selectCount(new LambdaQueryWrapper<StoreProductReply>().eq(StoreProductReply::getProductId, productId).lt(StoreProductReply::getProductScore, 5).gt(StoreProductReply::getProductScore, 2)));
            int poorCount = Math.toIntExact(((StoreProductReplyMapper) this.baseMapper).selectCount(new LambdaQueryWrapper<StoreProductReply>().eq(StoreProductReply::getProductId, productId).le(StoreProductReply::getProductScore, 2)));
            String replyChance = "" + NumberUtil.round(NumberUtil.mul(NumberUtil.div((float) goodCount, (float) sumCount), 100.0F), 2);
            String replyStar = "" + NumberUtil.round(NumberUtil.mul(NumberUtil.div((float) goodCount, (float) sumCount), 5.0F), 2);
            return ReplyCountVo.builder().sumCount(sumCount).goodCount(goodCount).inCount(inCount).poorCount(poorCount).replyChance(replyChance).replySstar(replyStar).build();
        }
    }

    public StoreProductReplyQueryVo handleReply(StoreProductReplyQueryVo replyQueryVo) {
        //购物车Vo
        StoreCartQueryVo cartInfo = (StoreCartQueryVo) JSONObject.parseObject(replyQueryVo.getCartInfo(), StoreCartQueryVo.class);
        if (ObjectUtil.isNotNull(cartInfo) && ObjectUtil.isNotNull(cartInfo.getProductInfo()) && ObjectUtil.isNotNull(cartInfo.getProductInfo().getAttrInfo())) {
            replyQueryVo.setSku(cartInfo.getProductInfo().getAttrInfo().getSku());
        }

        BigDecimal star = NumberUtil.add(replyQueryVo.getProductScore(), replyQueryVo.getServiceScore());
        star = NumberUtil.div(star, 2);
        replyQueryVo.setStar(String.valueOf(star.intValue()));
        if (StrUtil.isEmpty(replyQueryVo.getComment())) {
            replyQueryVo.setComment("此用户没有填写评价");
        }

        return replyQueryVo;
    }

}

